using System.Collections.Generic;
using System.Text;

namespace NFreeMarker.Core
{
    public sealed class AttemptBlock : TemplateElement // NOTE: Was internal
    {
        private readonly TemplateElement _attemptBlock;
        private readonly TemplateElement _recoveryBlock;

        internal AttemptBlock(TemplateElement attemptBlock, TemplateElement recoveryBlock)
        {
            _attemptBlock = attemptBlock;
            _recoveryBlock = recoveryBlock;
            NestedElements = new List<TemplateElement>
            {
                attemptBlock,
                recoveryBlock
            };
        }

        internal override void Accept(Environment env)
        {
            env.Visit(_attemptBlock, _recoveryBlock);
        }

        public override string GetCanonicalForm()
        {
            var buf = new StringBuilder("<#attempt>");
            if (_attemptBlock != null)
            {
                buf.Append(_attemptBlock.GetCanonicalForm());
            }
            if (_recoveryBlock != null)
            {
                buf.Append(_recoveryBlock.GetCanonicalForm());
            }
            return buf.ToString();
        }

        public override string GetDescription()
        {
            return "attempt block";
        }
    }
}